package view.list.detailview;


import filesystem.MyFile;

import javax.swing.table.DefaultTableCellRenderer;
import java.awt.*;

import javax.swing.*;



/**
 * Title:        Java File Manager
 * Description:
 * Copyright:    Copyright (c) 2001
 * Company:      Home
 *
 * @author Giurgiu Sergiu
 * @version 1.0
 */

public class DetailsTableCellRenderer extends DefaultTableCellRenderer {


    public DetailsTableCellRenderer() {
        super();
    }

    @Override
    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
      	DetailsTableModel model=(DetailsTableModel)table.getModel();

  	MyFile selectedFile=model.getFileAt(row);
  	if(selectedFile!=null && selectedFile.isMarked()){
  		setForeground(Color.BLUE);
  	}else{
  		setForeground(table.getForeground());
  	}
    if (!(value instanceof MyFile)) {
            setIcon(null);
        } else {
            MyFile f = (MyFile) value;
            setIcon(getIcon(f));
        }
        final Component rendererComponent = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);


        return rendererComponent;
    }


    private Icon getIcon(MyFile file) {
        Icon icon = null;
        icon = file.getIcon();
        if (icon == null) {
            if (file.isDirectory()) {
                // System.out.println("name:"+file.getName()+", displayname="+file.getDisplayName());
                if ("..".equals(file.getDisplayName())) {
                    icon = (Icon) UIManager.get("FileChooser.upFolderIcon");
                } else {
                    icon = (Icon) UIManager.get("FileView.directoryIcon");
                }
            } else {
                icon = (Icon) UIManager.get("FileView.fileIcon");
            }
        }
        return icon;
  }
}
